package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(tags = "套餐相关接口")
@RequestMapping("/admin/setmeal")
public class SetmealCotroller {

    @Autowired
    private SetmealService setmealService;

    /**
     * 套餐新增
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")//key: setmealCache::100
    @ApiOperation(value = "套餐新增")
    public Result add(@RequestBody SetmealDTO setmealDTO){
        setmealService.addWithDish(setmealDTO);
        return Result.success();
    }


    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
@GetMapping("/page")
@ApiOperation("分页查询套餐")
    public  Result<PageResult> page (SetmealPageQueryDTO setmealPageQueryDTO){
    PageResult pageResult=  setmealService.pagewith(setmealPageQueryDTO);
    return  Result.success(pageResult);
}

    /**
     * 批量删除套餐
      * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public  Result delete(@RequestParam List<Long>ids){
      setmealService.delete(ids);
      return Result.success();
}

    /**
     * 根据id查寻套餐
     * @param id
     * @return
     */
   @GetMapping("/{id}")
   @ApiOperation("根据id查寻套餐")
    public  Result<SetmealVO>   findSetmealId(@PathVariable Long id){
       log.info("根据id查寻套餐,参数：{}",id);
    SetmealVO setmealVO = setmealService.findSetmealId(id);
    return  Result.success(setmealVO);
    }

    /**
     * 套餐修改-
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation(value = "套餐信息修改")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        setmealService.updateh(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐起售停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售停售")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public  Result stareOrStop(@PathVariable Integer status,Long id){
  setmealService.stareOrStop(status,id);
  return Result.success();
    }
}
